package com.mysoft.gfyx.marketdevelop.controller.dto.inspect;

import com.mysoft.gfyx.marketdevelop.mip.dto.TjCustom.SqlApiResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.TjMetric.QueryMetricResponseDto;
import lombok.Data;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

@Data
public class PieResponseDto implements Serializable {

    private List<Item> pie;

    @Data
    public static class Item implements Serializable {
        private String name;
        private int value;
    }

    public PieResponseDto() {
        pie = new LinkedList<>();
    }

    public void loadData(QueryMetricResponseDto dto) {
        dto.getData().getList().forEach(indicator -> {
            if (indicator.getIndicators_aggregations() == null || indicator.getIndicators_aggregations().size() == 0) {
                return;
            }

            indicator.getIndicators_aggregations().forEach(a -> {
                Item i = new Item();
                a.getDimensions().forEach(d -> {
                    i.setName(d.getVal());
                });
                i.setValue(Integer.valueOf(a.getIndicator_val()));
                pie.add(i);
            });
        });
    }

    public void loadData(SqlApiResponseDto dto) {
        dto.getData().getRows().forEach(r -> {
            Item i = new Item();
            i.setName(r.get("tag_value_name").toString());
            i.setValue(Integer.valueOf(r.get("c").toString()));
            pie.add(i);
        });
    }
}
